@extends('admin.pub.pub')
@section('title','课程修改')
@section('bread1','课程管理')
@section('bread2','课程修改')
@section('css')
    <link rel="stylesheet" type="text/css" href="/admin/lib/webuploader/0.1.5/webuploader.css">
@endsection
@section('content')

    <article class="page-container">
        <form method="post" action="{{ route('admin.course.update',['course'=>$course->id]) }}" class="form form-horizontal" id="form">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>课程名称：</label>
                <div class="formControls col-xs-8 col-sm-10">
                    <input type="text" class="input-text" value="{{ $course->name }}" placeholder="" id="name"
                           name="name">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>讲师：</label>
                <div class="formControls col-xs-8 col-sm-10">
                    <input type="text" class="input-text" value="{{ $course->teacher }}" placeholder="" id="teacher"
                           name="teacher">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>封面：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <div style="width: 100px;height: 100px;border: 1px solid black;">
                        <img src="{{ $course->cover??'/images/default.png' }}" alt="封面" style="width: 100%;height: 100%;">
                    </div>
                    <div id="filePicker">选择图片</div>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>分类：</label>
                <div class="formControls col-xs-8 col-sm-10">
                    <span class="select-box radius">
                        <select class="select" size="1" name="category_id" id="category_id">
                            <option value=""> -- 请选择 -- </option>
                            @foreach($data as $item)
                                <option
                                        @if($course->category->id == $item->id)
                                        selected
                                        @endif
                                        value="{{ $item->id }}">{{ $item->name }}</option>
                            @endforeach
                        </select>
                    </span>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>简介：</label>
                <div class="formControls col-xs-8 col-sm-10">
                    <script id="editor" name="intro" type="text/plain" style="width:100%;height:400px;">{!! $course->intro !!}</script>
                    </div>
                    </div>
            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                    @csrf
                    {{ method_field('PUT')}}
                    <button class="btn btn-secondary radius" type="submit" id="add">&nbsp;&nbsp;修改&nbsp;&nbsp;</button>
                    <button onClick="removeIframe();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
                </div>
            </div>
        </form>
    </article>
@endsection
@section('js')
    <script type="text/javascript" src="/admin/lib/ueditor/1.4.3/ueditor.config.js"></script>
    <script type="text/javascript" src="/admin/lib/ueditor/1.4.3/ueditor.all.min.js"> </script>
    <script type="text/javascript" src="/admin/lib/ueditor/1.4.3/lang/zh-cn/zh-cn.js"></script>
    <script src="/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
    <script type="text/javascript" src="/admin/lib/webuploader/0.1.5/webuploader.min.js"></script>
    <script>
        var ue = UE.getEditor('editor');
        $('#form').validate({
            rules: {
                name: {
                    required: true
                },
                teacher: {
                    required: true
                },
                category_id: {
                    required: true
                }
            },
            messages: {
                name: {
                    required: "请输入课程名称"
                },
                teacher: {
                    required: "请输入讲师姓名"
                },
                category_id: {
                    required: "请输入课程分类"
                }
            }
        });

        var hasFile = false;
        var uploader = WebUploader.create({
            swf: '/admin/lib/webuploader/0.1.5/Uploader.swf',

            // 文件接收服务端。
            server: '{{ route('admin.course.update',['course'=>$course->id]) }}',

            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: '#filePicker',

            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize: false,
            // 请求类型
            method :'put',
            // 只允许选择图片文件。
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            },
            // 设置文件上传域的name
            fileVal:'file'
        });
        // 携带数据
        uploader.on( 'uploadBeforeSend', function( object, data,header ) {
            // 修改data可以控制发送哪些携带数据。
            data.name = $("#name").val();
            data.teacher = $("#teacher").val();
            data.category_id = $("#category_id").val();
            data.intro = $("[name=intro]").val();
            data._token = '{{ csrf_token() }}';
        });
        // 成功触发
        uploader.on("uploadAccept", function(file, data) {
            if (data.status === 0) {
                layer.msg(data.msg,{
                    time:1000
                },function () {
                    location.replace(location.href);
                })
            }else{
                layer.alert(data.msg,function () {
                    location.replace(location.href);
                })
            }
        });
        //当文件被加入队列以后触发
        uploader.on("fileQueued", function(file) {
            hasFile = true;
        });
        // 点击上传
        $('#add').click(function () {
            if (hasFile) {
                uploader.upload();
            }else {
                $.ajax({
                    url:'{{ route('admin.course.update',['course'=>$course->id]) }}',
                    type:'put',
                    data:{
                        name : $("#name").val(),
                        teacher : $("#teacher").val(),
                        category_id : $("#category_id").val(),
                        intro : $("[name=intro]").val(),
                        _token : '{{ csrf_token() }}'
                    },
                    dataType:'json',
                    success(res) {
                        if (res.status === 0) {
                            $('#cover').attr('src',res.url);
                            layer.msg(res.msg,{
                                time:1500
                            },function () {
                                location.replace(location.href);
                            })
                        }else{
                            layer.alert(res.msg,function () {
                                location.replace(location.href);
                            })
                        }
                    }
                })
            }
            return false;
        });

        @if ($errors->any())
        layer.alert("{{ $errors->first() }}");
        @endif
        @if(session('status') === 1)
        layer.msg("{{ session('msg') }}");
        @elseif(session('status') === 0)
        layer.alert("{{ session('msg') }}");
        @endif
    </script>
@endsection